package www.openteam.com.api;


import android.content.res.Resources;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import www.openteam.com.OTApplication;
import www.openteam.com.R;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.bean.CustomOrder;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.DeleteInfo;
import www.openteam.com.bean.DeleteWholeInfo;
import www.openteam.com.bean.DeliverGoods;
import www.openteam.com.bean.InsertStatus;
import www.openteam.com.bean.OrderApproval;
import www.openteam.com.bean.PackTypeInfo;
import www.openteam.com.bean.PayableApproval;
import www.openteam.com.bean.PbProductInfo;
import www.openteam.com.bean.ProOrder;
import www.openteam.com.bean.ProStock;
import www.openteam.com.bean.ProducePro;
import www.openteam.com.bean.ProductInfo;
import www.openteam.com.bean.QueryCustom;
import www.openteam.com.bean.ReceivableApproval;
import www.openteam.com.bean.Result;
import www.openteam.com.bean.SomeCustomInfo;
import www.openteam.com.bean.TakeDeliverGoods;
import www.openteam.com.bean.User;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Created by  kylin
 */
public class UserManager {
    //服务器的地址

    //       ;
//    private static final String ENDPOINT = "http://192.168.0.105:5001/index.php/OpenTeam/";
//    private static final String ENDPOINT = "http://192.168.1.119:8090/index.php/OpenTeam/";
    private static Resources res = OTApplication.getmContext().getResources();
    private static final String ENDPOINT =  res.getString(R.string.endPoint_n);
    private static final int  DEFAUT_TIMEOUT= 500;

      static  OkHttpClient.Builder httpClientBuilder=new OkHttpClient.Builder();


    //联网
    private static final Retrofit sRetrofit = new Retrofit.Builder()
            .baseUrl(ENDPOINT)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClientBuilder.connectTimeout(DEFAUT_TIMEOUT , SECONDS).addNetworkInterceptor(
                 new HttpLoggingInterceptor().
                            setLevel(HttpLoggingInterceptor.Level.BODY)).build())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用RxJava作为回调适配器
            .build();


    private static final UserApiService apiManager = sRetrofit.create(UserApiService.class);


    /**
     * 获取用户名
     *
     * @return
     */
    public static Observable<User> getUserData(int pageSize) {

        return apiManager.getUserData(pageSize);
    }

    /**
     * 检查用户名密码是否匹配
     *
     * @param name     用户名
     * @param password 密码
     * @return
     */
    public static Observable<Result> login(String name, String password) {
        return apiManager.login(name, password);
    }





    /**
     * 送货进度查询
     *
     * @param rdsno
     * @param custid
     * @param startTime
     * @param endTime
     * @param PageSize
     * @return
     */
    public static Observable<DeliverGoods> queryDeliverGoods(String rdsno, String custid, long startTime, long endTime, int PageSize) {

        return apiManager.queryDeliverGoods(rdsno, custid, startTime, endTime, PageSize);
    }

    /**
     * 收货进度查询
     *
     * @param rdsno
     * @param custid
     * @param startTime
     * @param endTime
     * @param PageSize
     * @return
     */
    public static Observable<TakeDeliverGoods> queryTDeliverGoods(String rdsno, String custid, long startTime, long endTime, int PageSize) {

        return apiManager.queryTDeliverGoods(rdsno, custid, startTime, endTime, PageSize);
    }

    /**
     * @param rdsno
     * @param custid
     * @param code
     * @param startTime
     * @param endTime
     * @param PageSize
     * @return
     */
    public static Observable<ProducePro> queryProd(String rdsno, String custid, String code, long startTime, long endTime, int PageSize) {

        return apiManager.queryProd(rdsno, custid, code, startTime, endTime, PageSize);
    }

    /**
     * @param rdsno
     * @param PageSize
     * @return
     */
    public static Observable<ProStock> queryStock(String rdsno, int PageSize) {

        return apiManager.queryStock(rdsno, PageSize);
    }

    /**
     * 订单审核
     *
     * @param custorderid
     * @param custid
     * @param orderStatus
     * @param startTime
     * @param endTime
     * @param PageSize
     * @return
     */
    public static Observable<OrderApproval> queryOrder(String custorderid, String custid, String orderStatus, long startTime, long endTime, int PageSize) {
        return apiManager.queryOrder(custorderid, custid, orderStatus, startTime, endTime, PageSize);
    }

    /**
     * @param rdsno
     * @param code
     * @param orderStatus
     * @param startTime
     * @param endTime
     * @param PageSize
     * @return
     */
    public static Observable<ProOrder> queryPrOrder(String rdsno, String code, String orderStatus, long startTime, long endTime, int PageSize) {
        return apiManager.queryPrOrder(rdsno, code, orderStatus, startTime, endTime, PageSize);
    }


    /**
     * 应收款审核
     *
     * @param custid    客户编号
     * @param startTime 对账日期起
     * @param endTime   对账日期止
     * @param PageSize  页
     * @return 应收款bean
     * 192.168.1.6:8090/index.php/OpenTeam/ReceivableApproval/receivableApproval?vendno=SHPC0001&startDate=1330790400000&endDate=&pageSize=0
     */
    public static Observable<ReceivableApproval> queryReceivableApproval(String custid, long startTime, long endTime, int PageSize) {
        return apiManager.queryReceivableApproval(custid, startTime, endTime, PageSize);
    }

    /**
     * 应付款审核
     *
     * @param vendno    客户编号
     * @param startTime 对账日期起
     * @param endTime   对账日期止
     * @param PageSize  页
     * @return 应收款bean
     * 192.168.1.6:8090/index.php/OpenTeam/PayableApproval/payableApproval?vendno=SHPC0001&startDate=1330790400000&endDate=&pageSize=0
     */
    public static Observable<PayableApproval> queryPayableApproval(String vendno, long startTime, long endTime, int PageSize) {
        return apiManager.queryPayableApproval(vendno, startTime, endTime, PageSize);
    }

    /**
     * 进行审核
     *
     * @param id
     * @return 审核成功, 审核失败
     */
    public static Observable<ApprovalStatu> approvalStatu(int id) {
        return apiManager.approvalStatu(id);
    }

    /**
     * 进行反审核
     *
     * @param id
     * @return 反审核失败
     */
    public static Observable<ApprovalStatu> unapprovalStatu(int id) {
        return apiManager.unapprovalStatu(id);
    }

    /**
     * 进行审核
     *
     * @param id
     * @return 审核成功, 审核失败
     */
    public static Observable<ApprovalStatu> reApprovalStatu(int id) {
        return apiManager.reApprovalUpdate(id);
    }

    /**
     * 进行反审核
     *
     * @param id
     * @return 反审核失败
     */
    public static Observable<ApprovalStatu> unReApprovalStatu(int id) {
        return apiManager.unReApprovalUpdate(id);
    }

    /**
     * 进行审核
     *
     * @param id
     * @return 审核成功, 审核失败
     */
    public static Observable<ApprovalStatu> paApprovalStatu(int id) {
        return apiManager.paApprovalUpdate(id);
    }

    /**
     * 进行反审核
     *
     * @param id
     * @return 反审核失败
     */
    public static Observable<ApprovalStatu> unPaApprovalStatu(int id) {
        return apiManager.unPaApprovalUpdate(id);
    }

    /**
     * @param id
     * @return
     */
    public static Observable<ApprovalStatu> prApprovalUpdate(int id) {
        return apiManager.prApprovalUpdate(id);
    }

    /**
     * @param id
     * @return
     */
    public static Observable<ApprovalStatu> unprApprovalUpdate(int id) {
        return apiManager.unprApprovalUpdate(id);
    }

    /**
     * 材料查询
     *
     * @return
     */
    public static Observable<CustomType> queryMaterialTypelst() {
        return apiManager.queryMaterialTypelst();
    }


    public static Observable<ProductInfo> queryProductInfo(String code, String name, int PageSize) {
        return apiManager.queryProductInfo(code, name, PageSize);
    }


    public static Observable<QueryCustom> queryCocustomInfo(String code, String name, String Businessman, int PageSize) {
        return apiManager.queryCocustomInfo(code, name, Businessman, PageSize);
    }

    public static Observable<CustomType> queryCocustomTypeInfo(int pageSize) {
        return apiManager.queryCocustomTypeInfo(pageSize);
    }

    public static Observable<CustomType> queryCopayTypeInfo(int pageSize) {
        return apiManager.queryCopayTypeInfo(pageSize);
    }

    public static Observable<CustomType> queryMoneyTypeInfo(int pageSize) {
        return apiManager.queryMoneyTypeInfo(pageSize);
    }

    public static Observable<CustomType> querySendTypeInfo(int pageSize) {
        return apiManager.querySendTypeInfo(pageSize);
    }


    public static Observable<InsertStatus> insertCustomInfo(String code, String name,
                                                            String type, String paytime,
                                                            String moneytype, String SendAddress,
                                                            String connecter, String BusinessMan,
                                                            String CreditNumb, String cTel) {
        return apiManager.insertCustomInfo(code, name, type, paytime, moneytype, SendAddress, connecter, BusinessMan, CreditNumb, cTel);

    }

    public static Observable<InsertStatus> insertProductInfo(String code, String name,
                                                             String type, String ccode,
                                                             String length, String width,
                                                             String height, String unit,
                                                             String maxstock, String typename,
                                                             String CoverPage, String contentpage, String minstock) {
        return apiManager.insertProductInfo(code, name, type, ccode,
                length, width, height, unit, maxstock, typename, CoverPage, contentpage, minstock);

    }

    public static Observable<InsertStatus> updateProductInfo(String code, String name,
                                                             String type, String ccode,
                                                             String length, String width,
                                                             String height, String unit,
                                                             String maxstock, String typename,
                                                             String CoverPage, String contentpage, String minstock) {
        return apiManager.updateProductInfo(code, name, type, ccode,
                length, width, height, unit, maxstock, typename, CoverPage, contentpage, minstock);

    }

    public static Observable<InsertStatus> updateCustomInfo(String code, String name,
                                                            String type, String paytime,
                                                            String moneytype, String SendAddress,
                                                            String connecter, String BusinessMan,
                                                            String CreditNumb, String cTel, int id) {
        return apiManager.updateCustomInfo(code, name, type, paytime, moneytype, SendAddress,
                connecter, BusinessMan, CreditNumb, cTel, id);

    }

    public static Observable<InsertStatus> deleteCustomInfo(int id) {
        return apiManager.deleteCustomInfo(id);

    }

    public static Observable<InsertStatus> deleteProductInfo(String code) {
        return apiManager.deleteProductInfo(code);
    }


    public static Observable<CustomOrder> queryCustomOrderInfo(String rdsno,
                                                               String custid,
                                                               int pageSize) {
        return apiManager.queryCustomOrderInfo(rdsno, custid, pageSize);
    }


    public static Observable<DeleteInfo> deleteCustomOrderItemInfo(int zbid, String prodno) {
        return apiManager.deleteCustomOrderItemInfo(zbid, prodno);
    }

    public static Observable<DeleteWholeInfo> deleteCustomOrderInfo(int id) {
        return apiManager.deleteCustomOrderInfo(id);
    }

    public static Observable<PbProductInfo> queryPbproductInfo(String name, int pageSize) {
        return apiManager.queryPbproductInfo(name, pageSize);
    }

    /**
     * 搜索包装类型名称
     *
     * @param name     可以模糊查询,填入一部分名称
     * @param pageSize
     * @return
     */
    public static Observable<PackTypeInfo> queryPackTypeInfo(String name, int pageSize) {
        return apiManager.queryPackTypeInfo(name, pageSize);
    }


    /**
     * 搜索包装类型名称
     *
     * @param name     可以模糊查询,填入一部分名称
     * @param pageSize
     * @return
     */
    public static Observable<SomeCustomInfo> querySomeCustomInfo(String name, int pageSize) {
        return apiManager.querySomeCustomInfo(name, pageSize);
    }

    /**
     * 更新客户订单子项信息
     *
     * @param id
     * @param prodno
     * @param wraptype
     * @param paperinf
     * @param senddate
     * @param technicsinf
     * @param printcolor
     * @param paynumb
     * @param othernumb
     * @param price
     * @param eprodno
     * @return
     */
    //    senddate, wraptype, paperinf, technicsinf, printcolor, paynumb, othernumb
    //    , price , prodno , id)
    public static Observable<ApprovalStatu> updateCustomOrderItemInfo(
            int id, String prodno
            , String wraptype, String paperinf
            , String senddate, String technicsinf
            , String printcolor, String paynumb
            , String othernumb, String price
            , String eprodno
    ) {

        return apiManager.updateCustomOrderItemInfo(id, prodno, wraptype, paperinf, senddate, technicsinf, printcolor, paynumb, othernumb, price, eprodno);
    }
    public static Observable<ApprovalStatu> insertCustomOrderItemInfo(
            int id, String prodno
            , String wraptype, String paperinf
            , String senddate, String technicsinf
            , String printcolor, String paynumb
            , String othernumb, String price
    ) {

        return apiManager.insertCustomOrderItemInfo(id, prodno, wraptype, paperinf, senddate, technicsinf, printcolor, paynumb, othernumb, price);
    }


    public static Observable<ApprovalStatu> updateCustomOrderMainInfo(
            int id, String rdsno,
            String custid, String userid,
            String moneytype, String paytime
            , String sendtype, String custorderid
            , String alreadlypay, String inputdate
            , String inputer, String orderamount
            , String remark, String orderdate
            , String sendaddress, String istax
            , String taxrate, String invtype
            , String stype
    ) {
        return apiManager.updateCustomOrderMainInfo(id, rdsno, custid, userid, moneytype, paytime, sendtype, custorderid, alreadlypay, inputdate, inputer, orderamount, remark, orderdate, sendaddress, istax, taxrate, invtype, stype);
    }

    public static Observable<ApprovalStatu> insertCustomOrderMainInfo(
            String custid, String businessno,
             String userid,
            String moneytype, String paytime
            , String sendtype, String custorderid
            , String alreadlypay, String inputdate
            , String inputer, String orderamount
            , String remark, String orderdate
            , String sendaddress, String istax
            , String taxrate, String invtype
            , String stype
    ) {
        return apiManager.insertCustomOrderMainInfo( custid, businessno,userid,
                moneytype, paytime, sendtype, custorderid, alreadlypay, inputdate,
                inputer, orderamount, remark, orderdate, sendaddress, istax, taxrate,
                invtype, stype);
    }
}
